package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbGoodsImageMapper;
import com.bwie.mapper.TbProdImageMapper;
import com.bwie.mapper.TbUnitMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdImage;
import com.bwie.pojo.TbUnit;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.Result;
import com.bwie.vo.AttaVo;
import com.bwie.vo.ProdPageVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Action;
import java.util.Date;
import java.util.List;

/**
* @author 28242
* @description 针对表【tb_prod(商品表)】的数据库操作Service实现
* @createDate 2025-05-27 09:00:51
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    TbUnitMapper tbUnitMapper;

    @Autowired
    HttpServletRequest request;


    @Autowired
    TbProdImageMapper tbProdImageMapper;

    @Override
    public Result getProdList(ProdPageVo prodPageVo) {


        //构造分页参数
        Page<TbProd> page = new Page<>(prodPageVo.getPageNum(),prodPageVo.getPageSize());

        //分页查询
//        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
//        wrapper.lambda().orderByDesc(TbProd::getCreateTime);
//        Page<TbProd> tbProdPage = tbProdMapper.selectPage(page, wrapper);

        Page<TbProd> tbProdPage=   tbProdMapper.getProdList(page,prodPageVo);


        return Result.ok().setData(tbProdPage);
    }

    @Override
    public Result addProd(TbProd tbProd) {

        //校验输入商品的spu
        TbProd prod = tbProdMapper.selectOne(
                new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdSpu, tbProd.getProdSpu())
        );
        if (prod!=null){
            return Result.error().setMsg("spu已经重复");
        }

        TbProd prod1 = tbProdMapper.selectOne(
                new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName())
        );
        if (prod1!=null){
            return Result.error().setMsg("商品已经重复");
        }

        String token = request.getHeader("token");
        if (token==null){
            return Result.error().setMsg("t请重新登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String nickName = body.get("nickName", String.class);

        tbProd.setProdStatus(0);
        tbProd.setCreateBy(nickName);
        tbProd.setCreateTime(new Date());
        tbProdMapper.insert(tbProd);


        return Result.ok();
    }

    @Override
    public Result getUnitList() {

        List<TbUnit> tbUnits = tbUnitMapper.selectList(null);


        return Result.ok().setData(tbUnits);
    }

    @Override
    public Result deleteAll(List<TbProd> tbProds) {

        if (tbProds==null || tbProds.size()==0){
            return Result.error().setMsg("请选择商品");
        }

        tbProds.stream().forEach(item->{
            tbProdMapper.deleteById(item);
        });

        return Result.ok();
    }

    @Override
    public Result addProdAtta(AttaVo attaVo) {


                   TbProd tbProd = tbProdMapper.selectById(attaVo.getProdId());
        if (tbProd==null){
            return Result.error().setMsg("商品不存在");
        }

        tbProd.setProdVideo(attaVo.getVideoList());
        tbProd.setUpType(attaVo.getUpType());
        tbProd.setUpTime(attaVo.getUpTime());
        tbProd.setDownType(attaVo.getDownType());
        tbProd.setDownTime(attaVo.getDownTime());

        tbProdMapper.updateById(tbProd);

        tbProdImageMapper.deleteById(tbProd.getProdId());

        for (TbProdImage tbProdImage : attaVo.getImageList()) {
            tbProdImage.setProdId(tbProd.getProdId());
            tbProdImageMapper.insert(tbProdImage);
        }



        return Result.ok();
    }


}




